<?php

	include("Commons.php");
	
	$action = param('ac');
	
	if ( $action=='send' )
	{
		$email = param('email');
		$subject = param('subject');
		$message = stripslashes(param('message'));
		
		$message = ereg_replace("\n", "<br/>", $message);
		
		if ( empty($email) )
			$emailerror = getTerm('contact', 'empty_email_address');
	
		elseif ( ! eregi('^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-z]{2,6}$', $email) )
			$emailerror = getTerm('contact', 'bad_email_address');
			
		// Sujet facultatif
		// if ( empty($subject) )
		//	$subjecterror = getTerm('contact', 'empty_subject');
			
		if ( empty($message) )
			$messageerror = getTerm('contact', 'empty_message');
			
		if ( empty($emailerror) && empty($subjecterror) && empty($messageerror) )
		{
			$parameters = array($email, $subject, $message);
			
			if ( empty($subject) )
				$subject = '-';
		
			sendMail('contact', 'admin@penpalhub.com', 'Max', $parameters);
			
			$_SESSION['message'] = getTerm('contact', 'message_sent');
			$sent = 1;
		}
	}
	
	pushNavigationItem( getTerm('contact', 'contact'), '/contact/' );

	printHeader( getTerm('contact', 'contact'), getTerm('contact', 'contact') );

	if ( ! empty($_SESSION['message']) )
	{
		print "<div class=\"information\">". $_SESSION['message'] ."</div>";
		$_SESSION['message'] = '';
	}
	
	print '<div id="contact">';
	
	if ( $sent==1 )
	{
		print '<div class="field">';
		print '<strong>'. getTerm('contact', 'email') .'</strong>';
		print '<br/>'. $email;
		print '</p></div>';
		
		print '<div class="field">';
		print '<strong>'. getTerm('contact', 'subject') .'</strong>';
		print '<br/>'. $subject;
		print '</div>';
		
		print '<div class="field">';
		print '<strong>'. getTerm('contact', 'message') .'</strong>';
		print '<br/>'. $message;
		print '</div>';
		
		print '<p>'. fill( getTerm('contact', 'return_home') , formatUrl('/') ) .'</p>';
	}
	else
	{
		print '<form method="post" action="'. $_SERVER['REQUEST_URI'] .'">';
		
		print '<input type="hidden" name="ac" value="send" />';
		
		print '<div class="field'. (empty($emailerror) ? '' : ' error') .'">';
		print '<label for="email">'. getTerm('contact', 'email') .'</label>';
		print '<input type="text" name="email" id="email" value="'. $email .'" />';
		print (empty($emailerror) ? '' : "<div class=\"error-hint\">$emailerror</div>");
		print '</div>';
		
		print '<div class="field'. (empty($subjecterror) ? '' : ' error') .'">';
		print '<label for="subject">'. getTerm('contact', 'subject') .'</label>';
		print '<input type="text" name="subject" id="subject" value="'. $subject .'" />';
		print (empty($subjecterror) ? '' : "<div class=\"error-hint\">$subjecterror</div>");
		print '</div>';
		
		print '<div class="field'. (empty($messageerror) ? '' : ' error') .'">';
		print '<label for="message">'. getTerm('contact', 'message') .'</label>';
		print '<textarea name="message" id="message">'. $message .'</textarea>';
		print (empty($messageerror) ? '' : "<div class=\"error-hint\">$messageerror</div>");
		print '</div>';
		
		print '<input type="submit" value="'. getTerm('contact', 'send') .'" /> ';
		print '<input type="button" value="'. getTerm('contact', 'cancel') .'" /> ';
		
		print '</form>';
	}
	
	print '</div>';
	
	printFooter();

?>
